home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / OBJDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  3.9 KB  |  134 lines

  1. //==================================
  2. // PEDUMP - Matt Pietrek 1995
  3. // FILE: OBJDUMP.C
  4. //==================================
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include "common.h"
  9. #include "extrnvar.h"
  10.  
  11. typedef struct _i386RelocTypes
  12. {
  13.     WORD type;
  14.     PSTR name;
  15. } i386RelocTypes;
  16.  
  17. // ASCII names for the various relocations used in i386 COFF OBJs
  18. i386RelocTypes i386Relocations[] = 
  19. {
  20. { IMAGE_REL_I386_ABSOLUTE, "ABSOLUTE" },
  21. { IMAGE_REL_I386_DIR16, "DIR16" },
  22. { IMAGE_REL_I386_REL16, "REL16" },
  23. { IMAGE_REL_I386_DIR32, "DIR32" },
  24. { IMAGE_REL_I386_DIR32NB, "DIR32NB" },
  25. { IMAGE_REL_I386_SEG12, "SEG12" },
  26. { IMAGE_REL_I386_SECTION, "SECTION" },
  27. { IMAGE_REL_I386_SECREL, "SECREL" },
  28. { IMAGE_REL_I386_REL32, "REL32" }
  29. };
  30. #define I386RELOCTYPECOUNT (sizeof(i386Relocations) / sizeof(i386RelocTypes))
  31.  
  32. //
  33. // Given an i386 OBJ relocation type, return its ASCII name in a buffer
  34. //
  35. void GetObjRelocationName(WORD type, PSTR buffer, DWORD cBytes)
  36. {
  37.     DWORD i;
  38.     
  39.     for ( i=0; i < I386RELOCTYPECOUNT; i++ )
  40.         if ( type == i386Relocations[i].type )
  41.         {
  42.             strncpy(buffer, i386Relocations[i].name, cBytes);
  43.             return;
  44.         }
  45.         
  46.     sprintf( buffer, "???_%X", type);
  47. }
  48.  
  49. //
  50. // Dump the relocation table for one COFF section
  51. //
  52. void DumpObjRelocations(PIMAGE_RELOCATION pRelocs, DWORD count)
  53. {
  54.     DWORD i;
  55.     char szTypeName[32];
  56.     
  57.     for ( i=0; i < count; i++ )
  58.     {
  59.         GetObjRelocationName(pRelocs->Type, szTypeName, sizeof(szTypeName));
  60.         printf("  Address: %08X  SymIndex: %08X  Type: %s\n",
  61.                 pRelocs->VirtualAddress, pRelocs->SymbolTableIndex,
  62.                 szTypeName);
  63.         pRelocs++;
  64.     }
  65. }
  66.  
  67. //
  68. // top level routine called from PEDUMP.C to dump the components of a
  69. // COFF OBJ file.
  70. //
  71. void DumpObjFile( PIMAGE_FILE_HEADER pImageFileHeader )
  72. {
  73.     unsigned i;
  74.     PIMAGE_SECTION_HEADER pSections;
  75.     
  76.     DumpHeader(pImageFileHeader);
  77.     printf("\n");
  78.  
  79.     pSections = MakePtr(PIMAGE_SECTION_HEADER, (pImageFileHeader+1),
  80.                             pImageFileHeader->SizeOfOptionalHeader);
  81.  
  82.     DumpSectionTable(pSections, pImageFileHeader->NumberOfSections, FALSE);
  83.     printf("\n");
  84.  
  85.     if ( fShowRelocations )
  86.     {
  87.         for ( i=0; i < pImageFileHeader->NumberOfSections; i++ )
  88.         {
  89.             if ( pSections[i].PointerToRelocations == 0 )
  90.                 continue;
  91.         
  92.             printf("Section %02X (%.8s) relocations\n", i, pSections[i].Name);
  93.             DumpObjRelocations( MakePtr(PIMAGE_RELOCATION, pImageFileHeader,
  94.                                     pSections[i].PointerToRelocations),
  95.                                 pSections[i].NumberOfRelocations );
  96.             printf("\n");
  97.         }
  98.     }
  99.     
  100.     PCOFFSymbolTable = MakePtr(PIMAGE_SYMBOL, pImageFileHeader,
  101.                                 pImageFileHeader->PointerToSymbolTable);
  102.     COFFSymbolCount = pImageFileHeader->NumberOfSymbols;
  103.  
  104.     if ( fShowLineNumbers )
  105.     {
  106.         // Walk through the section table...
  107.         for (i=0; i < pImageFileHeader->NumberOfSections; i++)
  108.         {
  109.             // if there's any line numbers for this section, dump'em
  110.             if ( pSections->NumberOfLinenumbers )
  111.             {
  112.                 DumpLineNumbers( MakePtr(PIMAGE_LINENUMBER, pImageFileHeader,
  113.                                          pSections->PointerToLinenumbers),
  114.                                  pSections->NumberOfLinenumbers );
  115.                 printf("\n");
  116.             }
  117.             pSections++;
  118.         }
  119.     }
  120.     
  121.     if ( fShowSymbolTable )
  122.     {
  123.         DumpSymbolTable(PCOFFSymbolTable, COFFSymbolCount);
  124.         printf("\n");
  125.     }
  126.     
  127.     if ( fShowRawSectionData )
  128.     {
  129.         DumpRawSectionData( (PIMAGE_SECTION_HEADER)(pImageFileHeader+1),
  130.                             pImageFileHeader,
  131.                             pImageFileHeader->NumberOfSections);
  132.     }
  133. }
  134.